package cn.runagain.demo.wechatminiappdemo.utils;

/**
 * created by
 *
 * @author tuxianchao
 * @date 2018/7/2 下午6:10
 */

import cn.hutool.db.Session;
import org.apache.commons.codec.digest.DigestUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.*;

/**
 * AES加密
 *
 * @author liuyazhuang
 */
public class AES {

    public static boolean initialized = false;

    /**
     * AES解密
     *
     * @param content 密文
     * @return
     * @throws InvalidAlgorithmParameterException
     * @throws NoSuchProviderException
     */
    public byte[] decrypt(byte[] content, byte[] keyByte, byte[] ivByte) throws InvalidAlgorithmParameterException {
        initialize();
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
            Key sKeySpec = new SecretKeySpec(keyByte, "AES");
            // 初始化
            cipher.init(Cipher.DECRYPT_MODE, sKeySpec, generateIV(ivByte));
            byte[] result = cipher.doFinal(content);
            return result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static void initialize() {
        if (initialized) {
            return;
        }
        Security.addProvider(new BouncyCastleProvider());
        initialized = true;
    }

    // 生成iv
    public static AlgorithmParameters generateIV(byte[] iv) throws Exception {
        AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
        params.init(new IvParameterSpec(iv));
        return params;
    }


    public static void main(String[] args) {
        /**
         * sha1签名校验
         * signature = sha1( rawData + session_key )
         */
        String rawDataAndSessionKey = "{\"nickName\":\"叫我图图就可以了\",\"gender\":1,\"language\":\"zh_CN\",\"city\":\"Chengdu\",\"province\":\"Sichuan\",\"country\":\"China\",\"avatarUrl\":\"https://wx.qlogo.cn/mmopen/vi_32/iaadC5T6yx7YRGGHymJXxIm3oqibq4bic4mP1gSu2ibL2yUtesnbhhTQBOCLuGPBRQTynGx1FKHCtwsk3Ovcr9bGIA/132\"}" +
                "stX+F8y7sqYZBuyai+ofEQ==";
        String signature = "bb392847842eb08f7606d6e829892df26f960cc2";

        long start = System.currentTimeMillis();
        String result = DigestUtils.shaHex(rawDataAndSessionKey);
        System.out.println(signature.equals(result));
        System.out.println("耗时:" + (System.currentTimeMillis() - start) + "ms");
    }
}






